<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="100px" label-position="left">
			<el-form-item label="排序号">
				<el-input-number v-model="form.sortNo" placeholder="=排序号"></el-input-number>
			</el-form-item>
			<el-form-item label="接口名称" prop="title">
				<el-input v-model="form.title" placeholder="接口名称"></el-input>
			</el-form-item>
			<el-form-item label="数据源" prop="sourceId">
				<el-select v-model="form.sourceId" style="width: 100%">
					<el-option v-for="item in dbOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-divider>业务SQL</el-divider>
			<sc-code-editor ref="editor" v-model="form.sqlStr" mode="sql" :height="100" theme="darcula" :options="{autoRefresh: true}"></sc-code-editor>
			<el-form-item label="系统常量">
				<div class="el-form-item-msg">1、{ORG_ID} 机构码</div>
				<div class="el-form-item-msg">2、{ACCOUNT_ID} 当前用户账号</div>
				<div class="el-form-item-msg">3、{DEPT_ID} 当前用户部门（兼职部门时无效）</div>
				<div class="el-form-item-msg">4、{USER_LEVEL} 当前用户行政级别（兼职职务时无效）</div>
				<div class="el-form-item-msg">5、{KEYWORD} 查询关键词</div>
			</el-form-item>
			<el-divider>回显SQL</el-divider>
			<sc-code-editor ref="editor" v-model="form.showSqlStr" mode="sql" :height="100" theme="darcula" :options="{autoRefresh: true}"></sc-code-editor>
			<el-form-item label="系统常量">
				<div class="el-form-item-msg">1、{KEY_ARRAY} 将接口keyId参数字符串自动外成数组</div>
			</el-form-item>
			<el-form-item label="是否生效">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.remark" maxlength="150" placeholder="备注" size="200"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
			<el-button @click="visible=false">取消</el-button>
		</template>
	</el-dialog>
</template>

<script>
import {defineAsyncComponent} from "vue";

const scCodeEditor = defineAsyncComponent(() => import('@/components/scCodeEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		scCodeEditor
	},
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑'
			},
			dbOptions:[],
			form: {
				title: "",
				sortNo: 0,
				sourceId: '',
				sqlStr:"",
				showSqlStr:"",
				status: 1,
				remark: ''
			},
			visible: false,
			isSaveing: false,
			rules: {
				title: [
					{required: true, message: '接口名称不能为空', trigger: 'blur'}
				],
				sqlStr: [
					{required: true, message: '业务SQL不能为空', trigger: 'blur'}
				]
			},
		}
	},
	mounted() {
		this.getDbSourceList();
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.system.sysDbInterface.insertSysDbInterface.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.system.sysDbInterface.updateSysDbInterface.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		async getDbSourceList()
		{
			var res = await this.$API.system.dbSourceConfig.getDbSourceListForSelect.get();
			if(res.code == 200){
				let resV=[{
					value:'0',
					label:'系统数据源'
				}];
				this.dbOptions = resV.concat(res.data);
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//表单注入数据
		setData(data) {
			this.form.faceId = data.faceId;
			this.form.sortNo = data.sortNo;
			this.form.title = data.title;
			this.form.sourceId = data.sourceId;
			this.form.sqlStr = data.sqlStr;
			this.form.showSqlStr = data.showSqlStr;
			this.form.status = data.status;
			this.form.remark = data.remark
		}
	}
}
</script>
<style>
.customWidth {
	width: 40%;
}
</style>
